package com.rustic.server.controller.api;

import cn.hutool.core.collection.CollUtil;
import cn.xy.base.server.bean.IPage;
import cn.xy.base.server.utils.PageUtil;
import cn.xy.commons.annotation.ApiAuth;
import com.rustic.server.enums.LinkType;
import com.rustic.server.po.Food;
import com.rustic.server.query.FoodUserListQuery;
import com.rustic.server.service.CommentService;
import com.rustic.server.service.FoodService;
import com.rustic.server.vo.FoodWebVO;
import com.rustic.server.vo.list.FoodWebListVO;
import com.rustic.server.vo.FoodVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rustic.server.web.WebContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import cn.xy.commons.core.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 表[food]对应的前台接口
 *
 * @author auto
 */

@Slf4j
@RestController
@RequestMapping("/api/food")
@Api(tags = {"美食推荐接口"})
@ApiAuth(ignore = true)
public class FoodController {

    @Autowired
    private FoodService foodService;

    @Autowired
    private CommentService commentService;

    @ApiOperation(value = "美食推荐-列表")
    @GetMapping("/list")
    public Result<Page<FoodWebListVO>> list(FoodUserListQuery query) {
        LambdaQueryWrapper<Food> queryWrapper = foodService.toUserListWrapper(query);
        Page<Food> pageData = foodService.page(query.toPage(), queryWrapper);
        List<Food> foodList = pageData.getRecords();
        if (CollUtil.isEmpty(foodList)) {
            return Result.data(new Page<>());
        }
        int userId = WebContext.getUserId();
        List<FoodWebListVO> results = foodService.toWebListVo(foodList, userId);
        return Result.data(PageUtil.toPage(pageData, results));
    }

    @ApiOperation(value = "美食推荐-详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "美食推荐id"),
    })
    @GetMapping("/detail")
    public Result<FoodWebVO> detail(int id) {
        if (id <= 0) {
            return Result.error(FoodService.FOOD_NOT_EXIST);
        }
        FoodWebVO foodWebVO = foodService.getByIdToWeb(id);
        int userId = WebContext.getUserId();
        commentService.setInteractionStatus(foodWebVO, userId, foodWebVO.getId(), LinkType.FOOD);
        return Result.data(foodWebVO);
    }

}
